package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.LoginUser;
import com.ws.response.LoginUserPageResponse;
import com.ws.response.LoginUserResponse;
import com.ws.service.LoginUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ：ws
 * @date ：Created in 2023/11/15 15:48
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/loginUser")
public class LoginUserController {

    @Autowired
    LoginUserService loginUserService;

    /**
     * 登录用户查询接口
     * @param pageable
     * @param username
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "登录用户查询接口", notes = "登录用户查询接口", httpMethod = "GET", response = LoginUserPageResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "登录用户名称", dataType = "String", required = true)})
    public ResponseEntity<LoginUserPageResponse> getLoginUserPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="username",required=false) String username,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<LoginUser> loginUserPaasPage = loginUserService.getLoginUserPage(pageable,username,statusCd);
        return ResponseEntity.ok(new LoginUserPageResponse(loginUserPaasPage));
    }

    /**
     * 用户新增接口
     * @param loginUser
     * @return
     */
    @PostMapping("")
    public ResponseEntity<LoginUserResponse> saveLoginUser(@RequestBody @Valid LoginUser loginUser){
        LoginUser loginUser1 = loginUserService.saveLoginUser(loginUser);
        return ResponseEntity.ok(new LoginUserResponse(loginUser1));
    }

    /**
     * 查询对应用户的基本信息
     * @param loginUserId
     * @return
     */
    @GetMapping("/{loginUserId}/basicInfo")
    @ApiOperation(value = "查询对应用户的基本信息", notes = "查询对应用户的基本信息", httpMethod = "GET", response = LoginUserResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "loginUserId", value = "用户标识", dataType = "Long", required = true)})
    public ResponseEntity<LoginUserResponse> getBookBasicInfo(@PathVariable Long loginUserId){
        LoginUser loginUser= loginUserService.getLoginUserBasicInfo(loginUserId);
        return ResponseEntity.ok(new LoginUserResponse(loginUser));
    }

    /**
     * 删除对应的用户
     * @param loginUserId
     * @return
     */
    @PostMapping("/d/{loginUserId}")
    public ResponseEntity delBook(@PathVariable Long loginUserId)  {
        loginUserService.delLoginUser(loginUserId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的用户
     * @param loginUserId
     * @return
     */
    @PostMapping("/u/{loginUserId}")
    public ResponseEntity<LoginUserResponse> updateLoginUser(@PathVariable Long loginUserId,@RequestBody LoginUser loginUser)  {
        LoginUser loginUser1 = loginUserService.updateLoginUser(loginUserId,loginUser);
        return ResponseEntity.ok(new LoginUserResponse(loginUser1));
    }
}
